﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace 学生信息管理系统
{
    public partial class TeacherSMessage : Form
    {
        public TeacherSMessage()
        {
            InitializeComponent();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            string cn_str = pub.CN_STR;
            OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
            cn.Open();
            string select = "SELECT   学号, 姓名, 性别, 年龄, 备注  FROM      学生信息  WHERE   (学号 = '"+txtNo.Text+"')";
            OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
            OleDbDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                this.txtAge.Text = reader["年龄"].ToString();
                this.txtSex.Text = reader["性别"].ToString();
                this.txtMemo.Text = reader["备注"].ToString();
                this.txtName.Text = reader["姓名"].ToString();

                reader.Close();
            }
            cn.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string cn_str = pub.CN_STR;
            OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
            cn.Open();
            string select = "UPDATE  学生信息   SET        姓名 = '"+txtName.Text+"', 性别 = '"+txtSex.Text+"', 年龄 = '"+txtAge.Text+"', 备注 = '"+txtMemo.Text+"'   WHERE   (学生信息.学号 = '"+txtNo.Text+"')";
            OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
            OleDbDataReader reader = cmd.ExecuteReader();
            MessageBox.Show("更新成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);   
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
             string cn_str = pub.CN_STR;
             OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
            try
            {cn.Open();
                string select = "INSERT INTO 学生信息           (学号, 姓名, 性别, 年龄, 备注)     VALUES   ('" + txtNo.Text + "', '" + txtName.Text + "', '" + txtSex.Text + "', '" + txtAge.Text + "', '" + txtMemo.Text + "')";
                OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
                OleDbDataReader reader = cmd.ExecuteReader();
                MessageBox.Show("插入成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                reader.Close();
                cn.Close();
            }

            catch (Exception ex)
            {
                MessageBox.Show("插入失败，已有重复数据！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

            finally {
                cn.Close();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            string cn_str = pub.CN_STR;
            OleDbConnection cn = new OleDbConnection(cn_str);              //建立数据库连接
            cn.Open();
            string select = "DELETE FROM 学生信息     WHERE   (学生信息.学号 = '"+txtNo.Text+"')";
            OleDbCommand cmd = new OleDbCommand(select, cn);                 //建立数据库命令
            OleDbDataReader reader = cmd.ExecuteReader();
            MessageBox.Show("删除成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            reader.Close();
            this.Close();
           
        }

        
    }
}
